package com.android.launcher3;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DeleteZone extends LinearLayout implements DropTarget,
		DragController.DragListener {
	private static final String TAG = "DeleteZone";
	private final boolean mApplyIconHoverColorFilter;
	private boolean mDragEnter;
	private boolean mDragEnterForced;
	private DragLayer mDragLayer;
	private final boolean mDrawDeleteZoneBg;
	private View mHandle;
	private Animation mHandleInAnimation;
	private Animation mHandleOutAnimation;
	private ColorFilter mIconHoverColorFilter;
	private AnimationSet mInAnimation;
	private final int mInOutAnimationDuration;
	private final float mInOutAnimationTranslationRatio;
	private Launcher mLauncher;
	private final int[] mLocation;
	private AnimationSet mOutAnimation;
	private final RectF mRegion;
	private Drawable mRemoveBg;
	private Drawable mRemoveBgDrag;
	private TextView mRemoveIcon;
	private FrameLayout mRemoveView;
	private boolean mTrashMode;

	/**
	 * 完整.
	 * 
	 * @param context
	 * @param paramAttributeSet
	 */
	public DeleteZone(Context context, AttributeSet paramAttributeSet) {
		super(context, paramAttributeSet);

		this.mLocation = new int[2];

		this.mRegion = new RectF();

		this.mIconHoverColorFilter = new PorterDuffColorFilter(-1610678272,
				PorterDuff.Mode.SRC_ATOP);
		;

		TypedArray a = context.obtainStyledAttributes(paramAttributeSet,
				R.styleable.DeleteZone);
		this.mApplyIconHoverColorFilter = a.getBoolean(
				R.styleable.DeleteZone_applyIconHoverColorFilter, false);
		;

		this.mDrawDeleteZoneBg = a.getBoolean(
				R.styleable.DeleteZone_drawDeleteZoneBg, false);

		this.mInOutAnimationDuration = a.getInt(
				R.styleable.DeleteZone_inOutAnimationDuration, 200);

		this.mInOutAnimationTranslationRatio = a.getFloat(
				R.styleable.DeleteZone_inOutAnimationTranslationRatio, 1.0F);
		a.recycle();
		init();
	}

	/**
	 * 完整.
	 */
	private void createAnimations() {
		Object localObject;

		if (this.mInAnimation == null) {
			FastAnimationSet localFastAnimationSet1 = new FastAnimationSet();
			this.mInAnimation = localFastAnimationSet1;

			AccelerateInterpolator localAccelerateInterpolator1 = new AccelerateInterpolator();
			this.mInAnimation.setInterpolator(localAccelerateInterpolator1);

			AlphaAnimation localAlphaAnimation1 = new AlphaAnimation(0.0F, 1.0F);
			this.mInAnimation.addAnimation(localAlphaAnimation1);

			// Log.e("---- getOrientation ----", "" + getOrientation());
			// if (getOrientation() == 0) {
			if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				float f1 = this.mInOutAnimationTranslationRatio;
				int i = 0;
				float f2 = 0.0F;
				int j = 1;
				float f3 = 0.0F;
				TranslateAnimation localTranslateAnimation1 = new TranslateAnimation(
						0, 0.0F, i, f2, 1, f1, j, f3);
				this.mInAnimation.addAnimation(localTranslateAnimation1);
				this.mInAnimation.setStartOffset(this.mInOutAnimationDuration);
				this.mInAnimation.setDuration(this.mInOutAnimationDuration);
			} else {
				TranslateAnimation localTranslateAnimation1 = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, 1.0f,
						Animation.RELATIVE_TO_SELF, 0.0f,
						Animation.RELATIVE_TO_SELF, 0.0f,
						Animation.RELATIVE_TO_SELF, 0.0f);
				this.mInAnimation.addAnimation(localTranslateAnimation1);
				this.mInAnimation.setStartOffset(this.mInOutAnimationDuration);
				this.mInAnimation.setDuration(this.mInOutAnimationDuration);
			}
		}

		if (this.mHandleInAnimation == null) {
			localObject = new FastAnimationSet();

			AlphaAnimation localAlphaAnimation2 = new AlphaAnimation(0.0F, 1.0F);
			((AnimationSet) localObject).addAnimation(localAlphaAnimation2);

			if (getOrientation() != 0) {
			}

			if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				float f4 = this.mInOutAnimationTranslationRatio;
				int k = 0;
				float f5 = 0.0F;
				int m = 1;
				float f6 = 0.0F;
				TranslateAnimation localTranslateAnimation2 = new TranslateAnimation(
						0, 0.0F, k, f5, 1, f4, m, f6);
				((AnimationSet) localObject)
						.addAnimation(localTranslateAnimation2);
				this.mHandleInAnimation = ((Animation) localObject);

				this.mHandleInAnimation
						.setStartOffset(this.mInOutAnimationDuration);
				this.mHandleInAnimation
						.setDuration(this.mInOutAnimationDuration);
			} else {
				TranslateAnimation localTranslateAnimation2 = new TranslateAnimation(
						Animation.RELATIVE_TO_SELF, 1.0f,
						Animation.RELATIVE_TO_SELF, 0.0f,
						Animation.RELATIVE_TO_SELF, 0.0f,
						Animation.RELATIVE_TO_SELF, 0.0f);
				((AnimationSet) localObject)
						.addAnimation(localTranslateAnimation2);
				this.mHandleInAnimation = ((Animation) localObject);

				this.mHandleInAnimation
						.setStartOffset(this.mInOutAnimationDuration);
				this.mHandleInAnimation
						.setDuration(this.mInOutAnimationDuration);
			}

		}

		if (this.mOutAnimation == null) {
			FastAnimationSet localFastAnimationSet2 = new FastAnimationSet();
			this.mOutAnimation = localFastAnimationSet2;
			localObject = this.mOutAnimation;

			AccelerateInterpolator localAccelerateInterpolator2 = new AccelerateInterpolator();
			((AnimationSet) localObject)
					.setInterpolator(localAccelerateInterpolator2);

			AlphaAnimation localAlphaAnimation3 = new AlphaAnimation(1.0F, 0.0F);
			((AnimationSet) localObject).addAnimation(localAlphaAnimation3);

			if (getOrientation() != 0) {
			}

			float f7 = this.mInOutAnimationTranslationRatio;

			if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				((AnimationSet) localObject)
						.addAnimation(new FastTranslateAnimation(
								Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE,
								0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
								Animation.RELATIVE_TO_SELF, 1.0f));
			} else {
				((AnimationSet) localObject)
						.addAnimation(new FastTranslateAnimation(
								Animation.RELATIVE_TO_SELF, 0.0f,
								Animation.RELATIVE_TO_SELF, 1.0f,
								Animation.RELATIVE_TO_SELF, 0.0f,
								Animation.RELATIVE_TO_SELF, 0.0f));
			}

			long l5 = this.mInOutAnimationDuration;
			((AnimationSet) localObject).setDuration(l5);
		}

		if (this.mHandleOutAnimation == null) {
			mHandleOutAnimation = new FastAnimationSet();

			AccelerateInterpolator localAccelerateInterpolator2 = new AccelerateInterpolator();
			((AnimationSet) mHandleOutAnimation)
					.setInterpolator(localAccelerateInterpolator2);

			AlphaAnimation localAlphaAnimation4 = new AlphaAnimation(1.0F, 0.0F);
			((AnimationSet) mHandleOutAnimation)
					.addAnimation(localAlphaAnimation4);

			if (getOrientation() != 0) {
			}

			float f10 = this.mInOutAnimationTranslationRatio;
			if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
				((AnimationSet) mHandleOutAnimation)
						.addAnimation(new FastTranslateAnimation(
								Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE,
								0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
								Animation.RELATIVE_TO_SELF, 1.0f));
			} else {
				((AnimationSet) mHandleOutAnimation)
						.addAnimation(new FastTranslateAnimation(
								Animation.RELATIVE_TO_SELF, 0.0f,
								Animation.RELATIVE_TO_SELF, 1.0f,
								Animation.RELATIVE_TO_SELF, 0.0f,
								Animation.RELATIVE_TO_SELF, 0.0f));
			}
		}
	}

	/**
	 * 完整.
	 */
	private void init() {
		Resources res = getResources();
		this.mRemoveBg = res.getDrawable(R.drawable.homescreen_remove_bg);
		this.mRemoveBgDrag = res
				.getDrawable(R.drawable.homescreen_remove_bg_drag);
	}

	/**
	 * 完整.
	 * 
	 * @param paramTextView
	 * @param paramColorFilter
	 */
	private void setIconViewColorFilter(TextView paramTextView,
			ColorFilter paramColorFilter) {
		Drawable[] arrayOfDrawable = paramTextView.getCompoundDrawables();
		arrayOfDrawable[1].setColorFilter(paramColorFilter);

		Drawable localDrawable1 = arrayOfDrawable[0];
		Drawable localDrawable2 = arrayOfDrawable[1];
		Drawable localDrawable3 = arrayOfDrawable[2];
		Drawable localDrawable4 = arrayOfDrawable[3];
		paramTextView.setCompoundDrawables(localDrawable1, localDrawable2,
				localDrawable3, localDrawable4);
		paramTextView.invalidate();
	}

	/**
	 * 完整.
	 * 
	 * @param paramItemInfo
	 */
	private void setMode(ItemInfo paramItemInfo) {
		this.mDragEnterForced = false;
		this.mRemoveView.setVisibility(View.VISIBLE);
	}

	/**
	 * 完整.
	 */
	public boolean acceptDrop(DragSource source, int x, int y, int xOffset,
			int yOffset, Object dragInfo) {
		LogUtils.d(TAG, "acceptDrop");

		return true;
	}

	/**
	 * 完整.
	 * 
	 * @param paramCanvas
	 */
	protected void dispatchDraw(Canvas paramCanvas) {
		// Log.e(TAG,"dispatchDraw");
		if (this.mDrawDeleteZoneBg) {
			Drawable localDrawable;

			if ((!this.mDragEnter) && (!this.mDragEnterForced)) {
				localDrawable = mRemoveBg;
			} else {
				localDrawable = mRemoveBgDrag;
			}

			int i = getWidth();
			int j = getHeight();
			localDrawable.setBounds(0, 0, i, j);
			localDrawable.draw(paramCanvas);
		}

		super.dispatchDraw(paramCanvas);
	}

	/**
	 * 完整.
	 * 
	 * @param paramDragSource
	 * @param paramInt1
	 * @param paramInt2
	 * @param paramInt3
	 * @param paramInt4
	 * @param paramObject
	 * @param paramRect
	 * 
	 * @return
	 */
	public Rect estimateDropLocation(DragSource paramDragSource, int paramInt1,
			int paramInt2, int paramInt3, int paramInt4, Object paramObject,
			Rect paramRect) {
		return null;
	}

	/**
	 * 完整.
	 * 
	 * @param rect
	 */
	public void getDeleteAreaRect(Rect rect) {
		if (rect == null) {
			return;
		}

		this.mRemoveView.getHitRect(rect);
	}

	/**
	 * 完整.
	 * 
	 * @param v
	 * 
	 * @return
	 */
	public int getDragAnimationXOffset(View v) {
		return 0;
	}

	/**
	 * 完整.
	 */
	public void onDragEnd() {
		LogUtils.d(TAG, "onDragEnd");

		if (this.mTrashMode) {
			this.mTrashMode = false;
			this.mDragLayer.setDeleteRegion(null);
			startAnimation(this.mOutAnimation);
			this.mHandle.startAnimation(this.mHandleInAnimation);
			this.setVisibility(View.GONE);
			this.mHandle.setVisibility(View.VISIBLE);
		}
	}

	/**
	 * 完整. (non-Javadoc)
	 * 
	 * @see com.android.launcher.DropTarget#onDragEnter(com.android.launcher.DragSource
	 *      , int, int, int, int, java.lang.Object)
	 */
	public void onDragEnter(DragSource source, int x, int y, int xOffset,
			int yOffset, Object dragInfo) {
		LogUtils.d(TAG, "onDragEnter");
		this.mDragEnter = true;

		if (this.mApplyIconHoverColorFilter) {
			setIconViewColorFilter(this.mRemoveIcon, this.mIconHoverColorFilter);
		}

		invalidate();
	}

	/**
	 * 完整. (non-Javadoc)
	 * 
	 * @see com.android.launcher.DropTarget#onDragExit(com.android.launcher.DragSource
	 *      , int, int, int, int, java.lang.Object)
	 */
	public void onDragExit(DragSource paramDragSource, int paramInt1,
			int paramInt2, int paramInt3, int paramInt4, Object paramObject) {
		LogUtils.d(TAG, "onDragExit");
		this.mDragEnter = false;
		this.mDragLayer.setTrashPaint(0);

		if (this.mApplyIconHoverColorFilter) {
			setIconViewColorFilter(this.mRemoveIcon, null);
		}

		invalidate();
	}

	/**
	 * 完整.
	 */
	public void onDragOver(DragSource source, int x, int y, int xOffset,
			int yOffset, Object dragInfo) {
		LogUtils.d(TAG, "onDragOver");
	}

	/**
	 * 完整.
	 */
	public void onDragStart(View v, DragSource source, Object info,
			int dragAction) {
		LogUtils.d(TAG, "onDragStart");

		ItemInfo localItemInfo = (ItemInfo) info;

		if (localItemInfo != null) {
			setMode(localItemInfo);
			this.mTrashMode = true;
			createAnimations();

			int[] arrayOfInt = this.mLocation;
			getLocationOnScreen(arrayOfInt);

			RectF localRectF1 = this.mRegion;
			float f1 = arrayOfInt[0];
			float f2 = arrayOfInt[1];
			int i = arrayOfInt[0];
			int j = this.mRight;
			int k = i + j;
			int m = this.mLeft;
			float f3 = k - m;
			int n = arrayOfInt[1];
			int i1 = this.mBottom;
			int i2 = n + i1;
			int i3 = this.mTop;
			float f4 = i2 - i3;
			localRectF1.set(f1, f2, f3, f4);

			DragLayer localDragLayer = this.mDragLayer;
			RectF localRectF2 = this.mRegion;
			localDragLayer.setDeleteRegion(localRectF2);
			startAnimation(mInAnimation);
			this.mHandle.startAnimation(mHandleOutAnimation);
			setVisibility(View.VISIBLE);
			this.mHandle.setVisibility(View.GONE);
		}
	}

	/**
	 * 完整. (non-Javadoc)
	 */
	public void onDrop(DragSource source, int x, int y, int xOffset,
			int yOffset, Object dragInfo) {
		LogUtils.d(TAG, "onDrop");

		// Log.e(TAG,"onDrop");
		final ItemInfo item = (ItemInfo) dragInfo;

		// Log.e(TAG,"onDrop item.container "+item.container);
		// Log.e(TAG,"onDrop  ItemInfo  "+item.getClass().getName());
		if (item.container == -1) {
			return;
		}

		// final LauncherModel model = Launcher.getModel();
		if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
			if (item instanceof LauncherAppWidgetInfo) {
				this.mLauncher.removeAppWidget((LauncherAppWidgetInfo) item);
			}

			if ((item instanceof ShortcutInfo)) {
				this.mLauncher.removeShortcut((ShortcutInfo) item);
			}

			if (item instanceof UserFolderInfo) {
				final UserFolderInfo userFolderInfo = (UserFolderInfo) item;
				LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher,
						userFolderInfo);
				this.mLauncher.removeFolder(userFolderInfo);
			}

			if ((item instanceof FolderInfo)) {
				this.mLauncher.removeFolder((FolderInfo) item);
			}
		} else {
			if (source instanceof UserFolder) {
				final UserFolder userFolder = (UserFolder) source;
				final UserFolderInfo userFolderInfo = (UserFolderInfo) userFolder
						.getInfo();
				userFolderInfo.remove((ShortcutInfo) item);
			}
		}

		if (item instanceof UserFolderInfo) {
			final UserFolderInfo userFolderInfo = (UserFolderInfo) item;
			LauncherModel.deleteUserFolderContentsFromDatabase(mLauncher,
					userFolderInfo);
			this.mLauncher.removeFolder(userFolderInfo);
		} else if (item instanceof LauncherAppWidgetInfo) {
			final LauncherAppWidgetInfo launcherAppWidgetInfo = (LauncherAppWidgetInfo) item;
			final LauncherAppWidgetHost appWidgetHost = mLauncher
					.getAppWidgetHost();

			if (appWidgetHost != null) {
				appWidgetHost
						.deleteAppWidgetId(launcherAppWidgetInfo.appWidgetId);
			}
		}

		LauncherModel.deleteItemFromDatabase(mLauncher, item);
	}

	/**
	 * 完整. (non-Javadoc)
	 * 
	 * @see android.view.View#onFinishInflate()
	 */
	protected void onFinishInflate() {
		super.onFinishInflate();
		this.mRemoveView = (FrameLayout) getChildAt(0);
		this.mRemoveIcon = (TextView) this.mRemoveView.getChildAt(0);
	}

	/**
	 * 完整.
	 */
	void resetMode() {
		setMode(null);
	}

	/**
	 * 完整.
	 * 
	 * @param paramDragLayer
	 */
	void setDragController(DragLayer paramDragLayer) {
		this.mDragLayer = paramDragLayer;
	}

	/**
	 * 完整.
	 * 
	 * @param paramBoolean
	 */
	void setDragEnterForced(boolean paramBoolean) {
		this.mDragEnterForced = paramBoolean;

		TextView localTextView;

		if (this.mApplyIconHoverColorFilter) {
			localTextView = this.mRemoveIcon;

			ColorFilter localColorFilter;

			if (this.mDragEnterForced) {
				localColorFilter = this.mIconHoverColorFilter;
			} else {
				localColorFilter = null;
			}

			setIconViewColorFilter(localTextView, localColorFilter);
		}
	}

	void setHandle(View v) {
		this.mHandle = v;
	}

	void setLauncher(Launcher launcher) {
		this.mLauncher = launcher;
	}

	private static class FastTranslateAnimation extends TranslateAnimation {
		/**
		 * Creates a new FastTranslateAnimation object.
		 */
		public FastTranslateAnimation(int fromXType, float fromXValue,
				int toXType, float toXValue, int fromYType, float fromYValue,
				int toYType, float toYValue) {
			super(fromXType, fromXValue, toXType, toXValue, fromYType,
					fromYValue, toYType, toYValue);
		}

		@Override
		public boolean willChangeTransformationMatrix() {
			return true;
		}

		@Override
		public boolean willChangeBounds() {
			return false;
		}
	}

	private static class FastAnimationSet extends AnimationSet {
		/**
		 * Creates a new FastAnimationSet object.
		 */
		FastAnimationSet() {
			super(false);
		}

		@Override
		public boolean willChangeTransformationMatrix() {
			return true;
		}

		@Override
		public boolean willChangeBounds() {
			return false;
		}
	}
}
